<?php 
namespace Admin\Model;
use Think\Model;

class CalendarModel extends Model {
	
	protected $_validate = array(
			array('title', 'require', '标题不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
			array('over_time', 'checkover', '截止时间不能小于当前日期', self::MUST_VALIDATE ,'callback', self::MODEL_BOTH),
			array('content', 'require', '内容不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
	);
	
	protected $_auto = array(
			array('uid', 'is_login', self::MODEL_INSERT, 'function'),
			array('create_time', NOW_TIME, self::MODEL_INSERT, 'string'),
			array('update_time', NOW_TIME, self::MODEL_BOTH, 'string'),
			array('over_time', 'strtotime', self::MODEL_BOTH, 'function'),
			array('status', '1',self::MODEL_INSERT, 'string'),
			array('file_id', 'get_file', self::MODEL_BOTH, 'callback')
	);
	
	protected function checkover($time) {
		$today = strtotime(date('Y-m-d',time()));
		$time  = strtotime($time);
		if ($time < $today) {
			return false;
		}else {
			return true;
		}
	}
	
	protected function get_file() {
		$file = json_decode(think_decrypt(I('post.file_id')), true);
		if(!empty($file)){
			$file_id = $file['id'];
		}else {
			$file_id = 0;
		}
		return $file_id;
	}
	
	function detail($id) {
		$map['status'] = 1;
		$info = $this->where($map)->find($id);
		if ($info['file_id'] > 0) {
			$info['file_name'] = M('File')->getFieldById($info['file_id'],'name');
		}
		return $info;
	}
	
	function calendarInfo($id) {
		$map['to_uid'] = UID;
		$map['cal_id'] = $id;
		$info = M('CalendarAccess')->where($map)->find();
		if ($info['return_id'] > 0) {
			$info['return_name'] = M('File')->getFieldById($info['return_id'],'name');
		}
		$calInfo = $this->detail($id);
		return array_merge($info, $calInfo);
	}
}